package cn.com.xyc.Controller;

import java.io.IOException;
import java.util.Date;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import cn.com.xyc.pojo.User;

@ContextConfiguration(locations = {
        "classpath:spring/spring-cache.xml"})
public class CacheController extends BaseController {
	@Autowired
	private MemcachedClient clientManager;

	@Test
	public void test1() {
		try {
			User admin1 = clientManager.get("currentUser1");
			System.out.println(admin1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test2() {
		try {
			User admin = new User();
			admin.setId("1");
			admin.setCreateDate(new Date());
			admin.setPassword("123456");
			admin.setStatus(1);
			admin.setUsername("xuyuancheng1");
			clientManager.set("currentUser1", 0, admin);//0表示存在 时间为1个月
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void test3() throws IOException {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("192.168.1.128:11211"));
		MemcachedClient client = builder.build();
		try {
			// client.set("hello", 0, "Hello,xmemcached");
			String value = client.get("username");
			System.out.println("值 是:" + value);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			client.shutdown();
		}
	}
}
